<template>
    <view>{{ uid }}</view>
</template>
<script>
export default {
    onLoad(args) {
        // const adapter = wx.getNFCAdapter();
        // adapter.onDiscovered(res => {
        //     console.log("读取到卡片了", res);
        //     let tagId = res.id; //如果是M1卡，这个ID是需要用于读取的秘钥
        //     let ssid = _this.buf2hex(tagId) //转换数据格式
        //     console.log("获取到tagID：", ssid);
        //     this.setData({ssid});
        //     this.isCardTypeRead(res,adapter);
        //   })

    },
    onShow() {
        this.nfc();

    },
    data() {
        return {
            year: "",
            base: "",
            chartData: {},
            chartDatas: {},
            title: "",
            uid:"暂无"
        };
    },
    methods: {
        nfc() {
            let NFCAdapter = wx.getNFCAdapter();
            NFCAdapter.startDiscovery({
                success: (res) => {
                    this.title = "请将设备放入识别区NFC";
                    console.log(res);
                },
                fail: (error) => {
                    this.title = "刷新重试";
                    console.error(error);
                },
                complete: (res) => {
                    console.log(res, '完成');
                },
            });
            NFCAdapter.onDiscovered(callback => {
                console.log(callback,'callback');
                
                let aid = parseInt(this.ab2hex(callback.id), 16);
                this.uid = aid;
                console.log(aid,'获取到的id');
                
                if (callback.messages) {
                    let cordsArray = callback.messages[0].records;
                    cordsArray.find(item => {
                        this.read.payload = byteToString(new Uint8Array(item.payload));
                        this.read.id = byteToString(new Uint8Array(item.id));
                        this.read.type = byteToString(new Uint8Array(item.type));
                    });
                }

            });
        },
        ab2hex(buffer) {
            var hexArr = Array.prototype.map.call(
                new Uint8Array(buffer),

                function (bit) {
                    return ('00' + bit.toString(16)).slice(-2);
                }
            );
            return hexArr.join('');
        },


    },
};
</script>